<?php
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../Entity/Info/Notification.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../Entity/Domain/EmailDomain.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../DAO/EmailDAO.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../Manager/SendEmail.php";
class EmailService {
	
	private $SMTP_Host;
	private $SMTP_Port;
	private $auth_username;
	private $auth_password;
	private $siteURL;
	
	public function setSiteURL($siteURL){
		$this->siteURL = $siteURL;
	}
	
	function __construct($SMTP_Host, $SMTP_Port, $auth_username, $auth_password) {
		$this->SMTP_Host = $SMTP_Host;
		$this->SMTP_Port = $SMTP_Port;
		$this->auth_username = $auth_username;
		$this->auth_password = $auth_password;
	}
	
	public function findAllEmail(BaseContext $context, ServiceResult $result)
	{
		$arrayList = array();
		try {
			$emailDAO = new EmailDAO($context);
			$dataList = $emailDAO->findAllEmail();
			
			foreach ($dataList as $data)
			{
				$emailDomain = new EmailDomain($data);
				$email = new Email($emailDomain);
				$countryService = new CountryServices();
				$countryDomain = $countryService->findAllByShortName($context, $result, $emailDomain->getLang());
				$email->setCountryDomain($countryDomain);
				array_push($arrayList, $email);
			}
			$result->setSuccess();
	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	return $arrayList;
	}
	
	public function findEmailByOid(BaseContext $context, ServiceResult $result, $oid)
	{
		$email = null;
		try {
			$emailDAO = new EmailDAO($context);
			$data = $emailDAO->findEmailByOid($oid);
			$emailDomain = new EmailDomain($data);
			$email = new Email($emailDomain);
			$countryService = new CountryServices();
			$countryDomain = $countryService->findAllByShortName($context, $result, $emailDomain->getLang());
			$email->setCountryDomain($countryDomain);
			$result->setSuccess();
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	return $email;
	}
	
	public function findAllEmailByRange(BaseContext $context, ServiceResult $result, $start, $limt)
	{
		$arrayList = array();
		try {
			$emailDAO = new EmailDAO($context);
			$dataList = $emailDAO->findAllEmailByRange($start, $limt);
			
			foreach ($dataList as $data)
			{
				$emailDomain = new EmailDomain($data);
				$email = new Email($emailDomain);
				$countryService = new CountryServices();
				$countryDomain = $countryService->findAllByShortName($context, $result, $emailDomain->getLang());
				$email->setCountryDomain($countryDomain);
				array_push($arrayList, $email);
			}
			$result->setSuccess();
	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	return $arrayList;
	}
	
	public function countAllEmail (BaseContext $context, ServiceResult $result) 
	{
		$count = 0;
		try 
		{
			$emailDAO = new EmailDAO($context);

			$count = $emailDAO->countAllEmail();
			
			$result->setSuccess();		
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
		
		return $count;
	}
	
	public function updateEmailByObj (BaseContext $context, ServiceResult $result, $obj) 
	{
		try 
		{
			$emailDAO = new EmailDAO($context);

			$emailDAO->update($obj);
			
			$result->setSuccess();		
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	}
	
	public function createEmail (BaseContext $context, ServiceResult $result, $obj) 
	{
		try 
		{
			$emailDAO = new EmailDAO($context);

			$emailDAO->create($obj);
			
			$result->setSuccess();		
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	}
	
	public function deleteEmailByOid (BaseContext $context, ServiceResult $result, $oid) 
	{
		try 
		{
			$emailDAO = new EmailDAO($context);

			$emailDAO->deleteByOid($oid);
			
			$result->setSuccess();		
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	}
	
	public function sendEmailCollectionNotification($context, $result, $fromEmail, $schedule){
	    
		$notification = Notification::getEmailCollectionNotification($fromEmail, $schedule);
		$notification->setSiteURL($this->siteURL);
		$emailDao = new EmailDAO($context);
		$notification = $emailDao->fillEmailTemplate($notification);
		//print_r($notification);
		try{
			$sendMail = new SendEmail($this->SMTP_Host, $this->SMTP_Port, $this->auth_username, $this->auth_password);
			$sendMail->SendNotification($schedule->getMemberDomain()->getEmail(), $notification->getSubject(), $notification->getHtmlContent(), $notification->getTextContent(), $notification->getFromEmail(), $notification->getFromName());
			$result->setSuccess();
		}
		catch (Exception $e) 
        {
			$context->logException($e);
			$result->setException($e);
		}
	}
	
	public function sendEmailDeliveryNotification($context, $result, $fromEmail, $schedule){
	    
		$notification = Notification::getEmailDeliveryNotification($fromEmail, $schedule);
		$notification->setSiteURL($this->siteURL);
		$emailDao = new EmailDAO($context);
		$notification = $emailDao->fillEmailTemplate($notification);
		//print_r($notification);
		try{
			$sendMail = new SendEmail($this->SMTP_Host, $this->SMTP_Port, $this->auth_username, $this->auth_password);
			$sendMail->SendNotification($schedule->getMemberDomain()->getEmail(), $notification->getSubject(), $notification->getHtmlContent(), $notification->getTextContent(), $notification->getFromEmail(), $notification->getFromName());
			$result->setSuccess();
		}
		catch (Exception $e) 
        {
			$context->logException($e);
			$result->setException($e);
		}
	}
}
?>
